<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Menu extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
	}
	

	function index()
	{
		$this->menu_main();	
	}
	
	
	function menu_main()
	{
            //check if the user is logged in from the session cockies and if yes will load the member page
		//if ($this->session->userdata('c_logged_in'))
		//{
                    list ($menu) = $this->get_menu();                

                    if($menu != FALSE)
                    {
                           
                            $full_table['items'] = $menu->result_array();
                    }
                    else
                    {
                            // assign FALSE the movies array so the code can distinguish between if we have table or not and print the error if not
                            $full_table['items'] = FALSE;
                    }

                    $this->load->view('view_menu', $full_table);
		//}
		//else
		//{
			// the user will be redirected in case there is no cockie or failure login
		//	redirect('yogette/menu');
		//}
	}
        
	function get_menu()
	{
		//get the customer movies queue, 1 as an input will means movies
		
                $this->load->model('Model_menu', 'menu');
                $menu_list = $this->menu->get_menu();
                
		// return both the movies and games queue list
		return array ($menu_list);
	}
        
        function get_specials()
	{
            if ($this->session->userdata('c_logged_in'))
            {
              $cus_id = $this->session->userdata('cus_id');
              //$prod_id = $this->uri->segment(3);
                
                   
             $full_table['allitem'] =  $this->model_menu->model_get_specials_all();
             $full_table['breakitem'] =  $this->model_menu->model_get_specials_breakfast();
             $full_table['launchitem'] =  $this->model_menu->model_get_specials_launch();
             $full_table['dinneritem'] =  $this->model_menu->model_get_specials_dinner();
            
              // go back to the index to view the member main page after adding the movie to the queue
              $this->load->view('view_specials', $full_table);
            }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }
	}
        
        
        
        function view_toppings()
	{
            if ($this->session->userdata('c_logged_in'))
            {
              $cus_id = $this->session->userdata('cus_id');
              //$prod_id = $this->uri->segment(3);
                
                   
             $full_table['item'] =  $this->model_menu->model_get_toppings();
            
              // go back to the index to view the member main page after adding the movie to the queue
              $this->load->view('view_toppings', $full_table);
            }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }
	}
        
        function add_cart()
        {
           //check if the user is logged in from the session cockies and if yes will load the member page
            if ($this->session->userdata('c_logged_in'))
            {
              $cus_id = $this->session->userdata('cus_id');
              $prod_id = $this->uri->segment(3);
                
                   
              $this->model_menu->model_add_queue($cus_id, $prod_id );

              // go back to the index to view the member main page after adding the movie to the queue
              $this->index();
            }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }
        }

        function add_cart_special()
        {
           //check if the user is logged in from the session cockies and if yes will load the member page
            if ($this->session->userdata('c_logged_in'))
            {
              $cus_id = $this->session->userdata('cus_id');
              $prod_id = $this->uri->segment(3);
                
                   
              $this->model_menu->model_add_queue($cus_id, $prod_id );

              // go back to the index to view the member main page after adding the movie to the queue
              $this->get_specials();
            }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }
        }
        
        function add_product() { // done

         if ($this->session->userdata('e_logged_in'))
            {
             $this->load->model('Model_menu', 'menu');

            $data['array'] = $this->menu->get_all_menutype();

           // $data['error'] = array('error' => $this->upload->display_errors());

            $data['error'] = $this->upload->display_errors();
        
        // this rule to check the username validaty "username is required maximum length is 50 charecter minimum length is 4 lowered case and checked if in DB adn cleaned by xss filters" 
            $this->form_validation->set_rules('sku', 'Sku', 'required|trim|min_length[8]|xss_clean');
            $this->form_validation->set_rules('product_name', 'Product name', 'required|trim|max_length[50]|xss_clean');
            $this->form_validation->set_rules('slug', 'Slug', 'trim|min_length[3]|xss_clean');
            $this->form_validation->set_rules('routid', 'Rout Id', 'trim|min_length[3]|xss_clean');
            $this->form_validation->set_rules('product_desc', 'Product Description', 'required|trim|max_length[50]|xss_clean');
            $this->form_validation->set_rules('excerpt', 'Excerpt', 'trim|min_length[3]|xss_clean');
            $this->form_validation->set_rules('p_price', 'Product Price', 'required|numeric|xss_clean');
            $this->form_validation->set_rules('p_sale_price', 'Product Sale Price', 'required|numeric|xss_clean');
            $this->form_validation->set_rules('p_tax', 'Product Tax', 'required|numeric|xss_clean');
            $this->form_validation->set_rules('weight', 'Product weight', 'required|numeric|xss_clean');
            $this->form_validation->set_rules('p_stock', 'Product Stock Number', 'required|numeric|xss_clean');
            $this->form_validation->set_rules('p_image', 'Image', 'required|trim|xss_clean');
            $this->form_validation->set_rules('options', 'Options', 'trim|max_length[200]|xss_clean');

            $this->form_validation->set_rules('menuname', 'menu type', 'required');
            $this->form_validation->set_rules('p_type', 'product type', 'required');


        if ($this->form_validation->run() == FALSE) {
                // viewing the registration page to the user  
                $this->load->view('view_add_product', $data);
            } else {
                // posting all inputs to the variables  after successful validation for these inputs
                $sku = $this->input->post('sku');
                $product_name = $this->input->post('product_name');
                $slug = $this->input->post('slug');
                $routid = $this->input->post('routid');
                $product_desc = $this->input->post('product_desc');
                $excerpt = $this->input->post('excerpt'); 
                $p_price = $this->input->post('p_price');
                $p_sale_price = $this->input->post('p_sale_price');
                $p_tax = $this->input->post('p_tax');
                $weight = $this->input->post('weight');
                $p_stock = $this->input->post('p_stock');
                $p_image = $this->input->post('p_image');
                $options = $this->input->post('options');

                $p_type = $this->input->post('p_type');
                $menuname = $this->input->post('menuname');

                if ($this->model_menu->model_add_product($sku,  
                        $product_name, $slug, $routid, $product_desc, $excerpt, 
                        $p_price, $p_sale_price, $p_tax, $weight, 
                        $p_stock, $p_image, $options, $p_type, $menuname)) 
                    {
                        // redirect the user to the main loging page after successful addition
                        redirect('yogette/upload');
                    } else {
                        redirect('menu/add_product');       
                    }
            }
        }
            else
            {
                // the user will be redirected in case there is no cockie or failure login
		redirect('yogette/user/login');
            }
    }
        function logout()
	{
		/* to destroy the seesion and logout*/
		$this->session->sess_destroy();
                redirect('/yogette/menu');
	}
        
}